package com.cuc.platform.core.web.sysadmin.security;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cuc.platform.core.componet.security.SecurityFacadeService;
import com.cuc.platform.core.componet.security.ShiroDbRealm.ShiroUser;
import com.cuc.platform.core.componet.security.domain.SecurityMenu;
import com.cuc.platform.core.componet.security.domain.SecurityUser;


/**
 * @author peepul
 *
 * 权限管理facade控制类
 */
@Controller
@RequestMapping(value = "/mainPage")
public class MainPageController {
	
	/**
	 * 显示修改密码页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/password/view", method = RequestMethod.GET)
	public String viewEditPassword(){
		
		return "commons/editPassword";
	}
	
	@RequestMapping(value = "/password/edit", method = RequestMethod.POST)
	public String editPassword(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		if (null == currentUser ) return null;
		
		//获取当前登录用户
		ShiroUser shiroUser = (ShiroUser) currentUser.getPrincipal();  
		SecurityUser user = securityFacadeService.getUserByLoginName(shiroUser.loginName);
		
		String newPassword = request.getParameter("newPwd");
		user.setPlainPassword(newPassword);
		
		//生成盐值密码，并更新
		securityFacadeService.updatePassword(user);
		
		return "redirect:commons/editPassword";
	}
	
	/*
	 * 查询登录账号所对应的菜单列表数据
	 * 
	 * user->role->menu
	 */
	@RequestMapping(value = "/listMenuByUser", method = RequestMethod.GET)
	public String listMenuByUser(@RequestParam String username,Model model){
		List<SecurityMenu> list = securityFacadeService.listMenuByUser(username);	
		model.addAttribute("list", list);
		
		return "left";
	}

	/**
	 * Ioc
	 */
	@Resource
	private SecurityFacadeService securityFacadeService;
}
